package com.cn.wanxi.servlet;

import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.ProductModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 这个类（对象）主要是返回页面数据
 * 数据来源于model
 */
@WebServlet("/home")//这一个是前台访问后台的地址
public class HomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CompanyModel companyModel = new CompanyModel();
        //这里是以key-value的形式进行返回，req.setAttribute括号里面两个参数，第一个参数就是key，第二个参数就是value
//        key是我们在页面要调用的值，value是页面得到的值，key的名字随便定义，更具自己的意思
        req.setAttribute("companyModel", companyModel);//将值存入jsp的内置对象
//        jsp的九大内置对象--
        /*
         * page  页面对象
         * request  请求对象
         * response  响应对象
         * session   会话期对象
         * application  应用对象
         * pageContent
         * Exception  异常对象
         * out  输出对象
         * config  配置对象
         */


//        ------------------------将导航返回给页面-------------------------------

        NavModel navModel = new NavModel();
        List<NavModel> navModelList = navModel.getList();//这里讲返回的list集合进行接收，然后返回到页面
        req.setAttribute("navModelList", navModelList);
//        ----------------------------新闻信息--------------------------------
        NewsModel newsModel = new NewsModel();
        List<NewsModel> newsModelList = newsModel.getList();
        req.setAttribute("newsModelList", newsModelList);
//        ---------------------------产品信息--------------------------
        ProductModel productModel = new ProductModel();
        List<ProductModel> productModelList = productModel.getList();
        req.setAttribute("productModelList", productModelList);

//        页面转发和重定向
        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);//页面转发
//        resp.sendRedirect("/jsp/home.jsp");//重定向,跳转页面

    }
}
